package com.example.woke;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zhongcai.online.R;
import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.woke.method.Httploaddata;
import com.woke.method.MyApp;
import com.wokeMy.view.Mywalletnocard_Pass;
import com.wokeMy.view.WxPassActivity;
import com.wokeMy.view.ZfbPassActivity;
import com.wokeMy.view.turui.MyRealNameActivity;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

public class ReciverFActivity extends Activity implements OnClickListener,
		Callback {

	private GridView mGridView;
	private String shuchu = "", shanchu = "";
	private TextView mEditText;
	private TextView mTnocard;
	private MyApp application;
	private AlertDialog alertDialog;
	private Intent intent = new Intent ();
	private float intValue;
	private int threetyse = 0;

	private TextView mT1, mT2, mT3, mT4, mT5, mT6, mT7, mT8, mT9, mT0, mT11;
	private ImageView mT12;
	private ImageView mImag1, mImag2, mImag3, mImag4;
	private String aaaa = "";
	private TextView mText;
	private Handler mHandler;
	private RelativeLayout mBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		// setContentView(R.layout.activity_reciver_f);
		setContentView (R.layout.activity_zcai);
		intview ();

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume ();

	}

	private void intview() {

		application = (MyApp) getApplication ();
		Httploaddata.uploadFile (application);
		mHandler = new Handler (ReciverFActivity.this);
		mText = (TextView) findViewById (R.id.avreciver_edit_money1);

		mBar = (RelativeLayout) findViewById (R.id.avzcaif_frame);

		mT1 = (TextView) findViewById (R.id.avzcai_text_1);
		mT2 = (TextView) findViewById (R.id.avzcai_text_2);
		mT3 = (TextView) findViewById (R.id.avzcai_text_3);
		mT4 = (TextView) findViewById (R.id.avzcai_text_4);
		mT5 = (TextView) findViewById (R.id.avzcai_text_5);
		mT6 = (TextView) findViewById (R.id.avzcai_text_6);
		mT7 = (TextView) findViewById (R.id.avzcai_text_7);
		mT8 = (TextView) findViewById (R.id.avzcai_text_8);
		mT9 = (TextView) findViewById (R.id.avzcai_text_9);
		mT0 = (TextView) findViewById (R.id.avzcai_text_0);
		mT11 = (TextView) findViewById (R.id.avzcai_text_11);
		mT12 = (ImageView) findViewById (R.id.avzcai_text_12);

		mImag1 = (ImageView) findViewById (R.id.avzcai_image_1);
		mImag2 = (ImageView) findViewById (R.id.avzcai_image_2);
		mImag3 = (ImageView) findViewById (R.id.avzcai_image_3);
		mImag4 = (ImageView) findViewById (R.id.avzcai_image_4);

		mImag1.setOnClickListener (new OnClickListener () {

			@Override
			public void onClick(View v) {
				if (application.getDatas_load () == null) return;
				threetyse = 3;
				String money3 = mText.getText ().toString ().trim ();
				if (null != application.getDatas_load ().getUser_no_status ()
						&& application.getDatas_load ().getUser_no_status ()
						.equals ("1")) {
					Toast.makeText (ReciverFActivity.this, "还未进行实名认证",
							Toast.LENGTH_SHORT).show ();
					aralogid ("还未实名认证", "前往认证？", 2);
				} else if (null != application.getDatas_load ()
						.getUser_no_status ()
						&& application.getDatas_load ().getUser_no_status ()
						.equals ("2")) {
					Toast.makeText (ReciverFActivity.this, "实名认证审核中，无操作权限",
							Toast.LENGTH_SHORT).show ();
				} else {
					if (! TextUtils.isEmpty (money3)) {
						intValue = Float.parseFloat (money3);
						getpayid ("zhongcai");
					} else {
						Toast.makeText (ReciverFActivity.this, "金额不能为空",
								Toast.LENGTH_SHORT).show ();
					}
				}
			}
		});

		mImag2.setOnClickListener (new OnClickListener () {

			@Override
			public void onClick(View v) {
				//支付宝
				if (application.getDatas_load () == null) return;
				threetyse = 1;
				final String money2 = mText.getText ().toString ().trim ();
				if (null != application.getDatas_load ().getUser_no_status ()
						&& application.getDatas_load ().getUser_no_status ()
						.equals ("1")) {
					Toast.makeText (ReciverFActivity.this, "还未进行实名认证",
							Toast.LENGTH_SHORT).show ();
					aralogid ("还未实名认证", "前往认证？", 2);
				} else if (null != application.getDatas_load ()
						.getUser_no_status ()
						&& application.getDatas_load ().getUser_no_status ()
						.equals ("2")) {
					Toast.makeText (ReciverFActivity.this, "实名认证审核中，无操作权限",
							Toast.LENGTH_SHORT).show ();
				} else {
						if (! TextUtils.isEmpty (money2)) {
//							intValue = Float.parseFloat (money2);
//							intent.setClass (ReciverFActivity.this,
//									ChannelsActivity.class);
//							intent.putExtra ("payWay", "alibaba");
//							intent.putExtra ("userId", application.getDatas_load ().getId ());
//							intent.putExtra ("money", money2);
//							startActivity (intent);

							intValue = Float.parseFloat (money2);
							intent.setClass (ReciverFActivity.this,
									ZfbPassActivity.class);
							intent.putExtra ("money", intValue);
							startActivity (intent);

						} else {
							Toast.makeText (ReciverFActivity.this, "金额不能为空",
									Toast.LENGTH_SHORT).show ();
						}
				}
			}
		});
		mImag3.setOnClickListener (new OnClickListener () {

			@Override
			public void onClick(View v) {
				Log.e("微信","1");
				if (application.getDatas_load () == null) return;
				threetyse = 1;
				final String money2 = mText.getText ().toString ().trim ();
				if (null != application.getDatas_load ().getUser_no_status ()
						&& application.getDatas_load ().getUser_no_status ()
						.equals ("1")) {
					Toast.makeText (ReciverFActivity.this, "还未进行实名认证",
							Toast.LENGTH_SHORT).show ();
					aralogid ("还未实名认证", "前往认证？", 2);
				} else if (null != application.getDatas_load ()
						.getUser_no_status ()
						&& application.getDatas_load ().getUser_no_status ()
						.equals ("2")) {
					Toast.makeText (ReciverFActivity.this, "实名认证审核中，无操作权限",
							Toast.LENGTH_SHORT).show ();
				} else {
						if (! TextUtils.isEmpty (money2)) {
//							intValue = Float.parseFloat (money2);
//
//							intent.setClass (ReciverFActivity.this,
//									ChannelsActivity.class);
//							intent.putExtra ("payWay", "wechat");
//							intent.putExtra ("userId", application.getDatas_load ().getId ());
//							intent.putExtra ("money", money2);
//							startActivity (intent);


							intValue = Float.parseFloat (money2);
							intent.setClass (ReciverFActivity.this,
									WxPassActivity.class);
							intent.putExtra ("money", intValue);
							startActivity (intent);
							Log.e("微信","4");
						} else {
							Toast.makeText (ReciverFActivity.this, "金额不能为空",
									Toast.LENGTH_SHORT).show ();
						}
				}
			}
		});
		mImag4.setOnClickListener (new OnClickListener () {

			@Override
			public void onClick(View v) {
				if (application.getDatas_load () == null) return;
				threetyse = 2;
				String money2 = mText.getText ().toString ().trim ();
				if (null != application.getDatas_load ().getUser_no_status ()
						&& application.getDatas_load ().getUser_no_status ()
						.equals ("1")) {
					Toast.makeText (ReciverFActivity.this, "还未进行实名认证",
							Toast.LENGTH_SHORT).show ();
					aralogid ("还未实名认证", "前往认证？", 2);
				} else {
					if (! TextUtils.isEmpty (money2)) {
						intValue = Float.parseFloat (money2);
						// intValue = Integer.valueOf(money2).intValue();
						getpayid ("nocard");
					} else {
						Toast.makeText (ReciverFActivity.this, "金额不能为空",
								Toast.LENGTH_SHORT).show ();
					}
				}
			}
		});

		mT1.setOnClickListener (this);
		mT2.setOnClickListener (this);
		mT3.setOnClickListener (this);
		mT4.setOnClickListener (this);
		mT5.setOnClickListener (this);
		mT6.setOnClickListener (this);
		mT7.setOnClickListener (this);
		mT8.setOnClickListener (this);
		mT9.setOnClickListener (this);
		mT0.setOnClickListener (this);
		mT11.setOnClickListener (this);
		mT12.setOnClickListener (this);

		findViewById (R.id.avreciverf_image_back).setOnClickListener (this);
	}

	class Myadter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return 12;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			View layout = getLayoutInflater ().inflate (
					R.layout.item_gridview_number, null);
			TextView mTextView = (TextView) layout
					.findViewById (R.id.item_itme_nuber);
			if (position < 9) {
				mTextView.setText ("" + (position + 1));
			} else if (position == 9) {
				mTextView.setText (".");
			} else if (position == 10) {
				mTextView.setText ("0");
			} else if (position == 11) {
				mTextView.setText ("-");
			}

			return layout;
		}
	}

	private void aralogid(String info, String kll, final int ppp) {
		alertDialog = new AlertDialog.Builder (this)
				.setTitle (info)
				.setMessage (kll)
				.
				// setIcon(R.drawable.ic_launcher).
						setPositiveButton ("确定", new DialogInterface.OnClickListener () {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// Intent intent = new Intent();
						if (ppp == 1) {
							intent.setClass (ReciverFActivity.this,
									LoadActivity.class);
							startActivity (intent);
						} else if (ppp == 2) {
							intent.setClass (ReciverFActivity.this,
									MyRealNameActivity.class);
							startActivity (intent);
						}
						alertDialog.dismiss ();
					}
				})
				.setNegativeButton ("取消", new DialogInterface.OnClickListener () {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						alertDialog.dismiss ();
						// TODO Auto-generated method stub
					}
				}).create ();
		alertDialog.show ();
	}

	/**
	 * 中财支付和银联支付,微信和支付宝后台直接生产二维码
	 *
	 * @param platform
	 */
	private void getpayid(String platform) {
		if (application.getDatas_load () == null) return;
		AsyncHttpClient ahc = new AsyncHttpClient ();
		PersistentCookieStore myCookieStore = new PersistentCookieStore (
				ReciverFActivity.this);
		ahc.setCookieStore (myCookieStore);
		RequestParams params = new RequestParams ();
		params.put ("g", "api");
		params.put ("m", "order");
		params.put ("a", "createorder");
		params.put ("user_id", application.getDatas_load ().getId ());
		params.put ("money", "" + intValue);
		params.put ("platform", platform);
		Log.e ("test", "id is " + application.getDatas_load ().getId ()
				+ "         p;atform--" + platform);

		ahc.get (HttpMethods.BASEURL, params, new JsonHttpResponseHandler () {
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess (statusCode, headers, response);
				Log.e ("120", "response" + response);
				try {
					String flag = response.getString ("flag");
					if (flag.equals ("success")) {
						JSONObject jsonObject = response.getJSONObject ("data");
						JSONObject json = jsonObject.getJSONObject ("data");
						String order_id = json.getString ("order_id");
						String playform = json.getString ("platform");
						if (threetyse == 0) {
							intent.setClass (ReciverFActivity.this,
									AlipapapayActivity.class);
							intent.putExtra ("money", intValue);
							intent.putExtra ("order_id", order_id);
							intent.putExtra ("threetyse", threetyse);
							startActivity (intent);
						} else if (threetyse == 1) {
							intent.setClass (ReciverFActivity.this,
									AlipapapayActivity.class);
							intent.putExtra ("money", intValue);
							intent.putExtra ("order_id", order_id);
							intent.putExtra ("threetyse", threetyse);
							startActivity (intent);
						} else if (threetyse == 2) {
							intent.setClass (ReciverFActivity.this,
									Mywalletnocard_Pass.class);
							intent.putExtra ("money", intValue);
							Log.e("reciverF",intValue+"");
							intent.putExtra ("order_id", order_id);
							intent.putExtra ("playform", playform);
							startActivity (intent);
						} else {
							intent.setClass (ReciverFActivity.this,
									AlipapapayActivity.class);
							intent.putExtra ("money", intValue);
							intent.putExtra ("order_id", order_id);
							intent.putExtra ("threetyse", threetyse);
							startActivity (intent);
						}

					} else if (flag.equals ("unknow")) {
						String info = response.getString ("info");
						Toast.makeText (ReciverFActivity.this, info,
								Toast.LENGTH_SHORT).show ();
					}
				} catch (JSONException e) {
					e.printStackTrace ();
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure (statusCode, headers, responseString, throwable);
				Toast.makeText (ReciverFActivity.this, "数据失败！",
						Toast.LENGTH_SHORT).show ();
				Log.e ("借给他是吧dd", "失败" + responseString);
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart ();

				mBar.setVisibility (View.VISIBLE);
				mImag1.setClickable (false);
				mImag2.setClickable (false);
				mImag3.setClickable (false);
				mImag4.setClickable (false);

			}

			@Override
			public void onFinish() {
				super.onFinish ();
				mBar.setVisibility (View.GONE);
				mImag1.setClickable (true);
				mImag2.setClickable (true);
				mImag3.setClickable (true);
				mImag4.setClickable (true);
				// Log.e("������ǰ�", "���");
			}
		});
	}

	@Override
	public void onClick(View v) {

		Message msg = mHandler.obtainMessage ();
		switch (v.getId ()) {
			case R.id.avzcai_text_1:
				String t1 = mT1.getText ().toString ();
				aaaa = aaaa + t1;
				mText.setText (aaaa);
				msg.what = 0x101;
				mHandler.sendMessage (msg);
				break;

			case R.id.avzcai_text_2:
				String t2 = mT2.getText ().toString ();
				aaaa = aaaa + t2;
				mText.setText (aaaa);
				msg.what = 0x101;
				mHandler.sendMessage (msg);
				break;
			case R.id.avzcai_text_3:
				String t3 = mT3.getText ().toString ();
				aaaa = aaaa + t3;
				mText.setText (aaaa);
				msg.what = 0x101;
				mHandler.sendMessage (msg);
				break;
			case R.id.avzcai_text_4:
				String t4 = mT4.getText ().toString ();
				aaaa = aaaa + t4;
				mText.setText (aaaa);
				msg.what = 0x101;
				mHandler.sendMessage (msg);
				break;
			case R.id.avzcai_text_5:
				String t5 = mT5.getText ().toString ();
				aaaa = aaaa + t5;
				mText.setText (aaaa);
				msg.what = 0x101;
				mHandler.sendMessage (msg);
				break;
			case R.id.avzcai_text_6:
				String t6 = mT6.getText ().toString ();
				aaaa = aaaa + t6;
				mText.setText (aaaa);
				msg.what = 0x101;
				mHandler.sendMessage (msg);
				break;
			case R.id.avzcai_text_7:
				String t7 = mT7.getText ().toString ();
				aaaa = aaaa + t7;
				mText.setText (aaaa);
				msg.what = 0x101;
				mHandler.sendMessage (msg);
				break;
			case R.id.avzcai_text_8:
				String t8 = mT8.getText ().toString ();
				aaaa = aaaa + t8;
				mText.setText (aaaa);
				msg.what = 0x101;
				mHandler.sendMessage (msg);
				break;
			case R.id.avzcai_text_9:
				String t9 = mT9.getText ().toString ();
				aaaa = aaaa + t9;
				mText.setText (aaaa);
				msg.what = 0x101;
				mHandler.sendMessage (msg);
				break;
			case R.id.avzcai_text_0:
				String t0 = mT0.getText ().toString ();
				if (aaaa.equals ("")) {
					aaaa = "0.";
				} else {
					if (aaaa.indexOf (".") > - 1) {

					} else {
						aaaa = aaaa + t0;
					}
				}
				mText.setText (aaaa);
				msg.what = 0x101;
				mHandler.sendMessage (msg);
				break;
			case R.id.avzcai_text_11:
				String t11 = mT11.getText ().toString ();
				if (aaaa.equals ("")) {
					aaaa = "0.";
				} else {
					aaaa = aaaa + t11;
				}
				mText.setText (aaaa);
				msg.what = 0x101;
				mHandler.sendMessage (msg);
				break;
			case R.id.avzcai_text_12:
				// String t12 = mT1.getText().toString();
				if (aaaa.equals ("")) {
				} else if (aaaa.equals ("0.")) {
					aaaa = "";
				} else {
					aaaa = aaaa.substring (0, aaaa.length () - 1);
				}
				mText.setText (aaaa);
				msg.what = 0x101;
				mHandler.sendMessage (msg);
				break;

			case R.id.avreciverf_image_back:
				finish ();
				break;

			default:
				break;
		}
	}

	@Override
	public boolean handleMessage(Message msg) {
		if (msg.what == 0x101) {
			if (aaaa.equals ("")) {
				mImag1.setImageDrawable (getResources ().getDrawable (
						R.mipmap.jianp_oem));
				mImag2.setImageDrawable (getResources ().getDrawable (
						R.drawable.avzcai_llkl2));
				mImag3.setImageDrawable (getResources ().getDrawable (
						R.drawable.avzcai_llkl3));
				mImag4.setImageDrawable (getResources ().getDrawable (
						R.drawable.avzcai_llkl4));
			} else {
				mImag1.setImageDrawable (getResources ().getDrawable (
						R.mipmap.jianp_check_oem));
				mImag2.setImageDrawable (getResources ().getDrawable (
						R.drawable.avzcai_1));
				mImag3.setImageDrawable (getResources ().getDrawable (
						R.drawable.avzcai_2));
				mImag4.setImageDrawable (getResources ().getDrawable (
						R.drawable.avzcai_3));
			}
		}
		return false;
	}


}
